﻿using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void Indent()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression.Indent >= 6)
            {
                //这个消息框很烦人！！
                //MessageBox.Show("已到最大缩进级别！", Globals.appName,
                //    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                return;//子面板不支持缩进。
            }

            if (curEx is Expressions.BaseText && curEx.Indent < 6)
            {
                Expressions.BaseText curBt = curEx as Expressions.BaseText;
                if (curBt.IsCollapsed)
                {
                    MessageBox.Show("折叠的标题文本块不能更改缩进级别。此外“剪切、粘贴、删除、平移、拖动”等操作亦被禁止。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            if (curEx.Parent is StackPanel)
            {
                StackPanel spanel = curEx.Parent as StackPanel;

                if (spanel.Orientation == Orientation.Vertical)
                {
                    if (curEx.Align == Expressions.Expression.Alignment.RightOrBottom)
                    {
                        return;//右对齐时，Indent或Outdent均无效。
                    }

                    ModifingItem mi = new ModifingItem();
                    Action action = new Action(curEx.ExID, -1, curEx.GetType().Name, "Indent",
                        curEx.Indent.ToString(), (curEx.Indent + 1).ToString());
                    curEx.Indent += 1;

                    //如果缩进到上一个折叠的标题文本块的“下级”，则应展开上一个折叠的标题文本块。
                    Expressions.Expression preVisibleEx = null;
                    int curIndex = curEx.ParentPanel.BasePanel.Children.IndexOf(curEx);
                    for (int i = curIndex - 1; i >= 0; i--)
                    {
                        Expressions.Expression iEx = curEx.ParentPanel.BasePanel.Children[i] as Expressions.Expression;
                        if (iEx.Visibility == Visibility.Visible)
                        {
                            preVisibleEx = iEx;
                            break;
                        }
                    }

                    if (preVisibleEx != null)
                    {
                        if (preVisibleEx is Expressions.BaseText)
                        {
                            Expressions.BaseText preVisibleBt = preVisibleEx as Expressions.BaseText;
                            if (preVisibleBt.IsCollapsed && preVisibleBt.Indent == curEx.Indent - 1)
                            {
                                Action actUnCollapse = new Action(preVisibleBt.ExID, -1, preVisibleBt.GetType().Name,
                                    "IsCollapsed", true.ToString(), false.ToString());

                                preVisibleBt.IsCollapsed = false;

                                mi.Add(actUnCollapse);
                            }
                        }
                    }

                    mi.Add(action);

                    if (curEx.Indent >= 6)
                    {
                        Globals.MainWindow.btnIndent.IsEnabled = false;
                    }
                    else
                    {
                        Globals.MainWindow.btnIndent.IsEnabled = true;
                    }

                    Globals.MainWindow.btnOutdent.IsEnabled = true;

                    if (curEx is Expressions.BaseText)
                    {
                        Expressions.BaseText bt = curEx as Expressions.BaseText;
                        if (bt.IsHeaderText)
                        {
                            if (bt.Indent >= 4)//从这开始都是正文字号大小。
                            {
                                Action actFontSizeLevel = new Action(bt.ExID, -1,
                                    bt.GetType().Name,
                                    "FontSizeLevel", bt.FontSizeLevel.ToString(), "0");
                                //正文的“FontSizeLevel”是０级，而４级标题则是１级。
                                bt.FontSizeLevel = 0;

                                mi.Add(actFontSizeLevel);

                                //只有isHesaText为真时才有必要控制加粗。
                                if (bt.FontWeight == FontWeights.Bold)
                                {
                                    Action actFontWeight = new Action(bt.ExID, -1,
                                        bt.GetType().Name, "FontWeight",
                                        bt.FontWeight.ToString(),
                                        FontWeights.Normal.ToString());
                                    bt.FontWeight = FontWeights.Normal;
                                    mi.Add(actFontWeight);
                                }
                            }
                            else if (bt.Indent > 0)//Indent操作不可能出现０级。
                            {
                                Action actFontSizeLevel = new Action(bt.ExID, -1,
                                    bt.GetType().Name,
                                    "FontSizeLevel", bt.FontSizeLevel.ToString(),
                                    (bt.FontSizeLevel - 1).ToString());
                                //正文的“FontSizeLevel”是０级，而４级标题则是１级。
                                bt.FontSizeLevel--;

                                mi.Add(actFontSizeLevel);

                                //只有isHesaText为真时才有必要控制加粗。
                                //if (bt.FontSizeLevel % 2 == 0)
                                if (bt.FontSizeLevel == 4)
                                {
                                    if (bt.FontWeight != FontWeights.Bold)
                                    {
                                        Action actFontWeight = new Action(bt.ExID, -1,
                                            bt.GetType().Name, "FontWeight",
                                            bt.FontWeight.ToString(),
                                            FontWeights.Bold.ToString());
                                        bt.FontWeight = FontWeights.Bold;
                                        mi.Add(actFontWeight);
                                    }
                                }
                                else
                                {
                                    if (bt.FontWeight != FontWeights.Normal)
                                    {
                                        Action actFontWeight = new Action(bt.ExID, -1,
                                            bt.GetType().Name, "FontWeight",
                                            bt.FontWeight.ToString(),
                                            FontWeights.Normal.ToString());
                                        bt.FontWeight = FontWeights.Normal;
                                        mi.Add(actFontWeight);
                                    }
                                }
                            }
                        }

                        Globals.MainWindow.tbtnBold.IsChecked = (bt.FontWeight == FontWeights.Bold ? true : false);
                        Globals.MainWindow.tbtnItalic.IsChecked = (bt.FontStyle == FontStyles.Italic ? true : false);

                        //重复。BaseText本身就会调用这个方法。
                        //bt.ParentPanel.RefreshAutoNumbersOfBaseText();
                        bt.RefreshText();

                        Globals.MainWindow.RefreshFontSizeLevel(bt.FontSizeLevel);
                    }

                    // 刷新父面板中所有可能存在的树型线。
                    if (curEx.ParentPanel != null)
                    {
                        curEx.ParentPanel.RefreshTreeLinesOfHeaderText();
                    }

                    curEx.Editor.Do(mi);
                }
                else
                {
                    //水平子面板中，不支持左缩进。
                    MessageBox.Show("在垂直子面板中的公式才支持“增加左缩进”操作！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }

        private static bool CheckForGetAndSetIndent()
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return false;
            if (Globals.selectedEditor.SelectedExpression is Expressions.SubPanel)
            {
                MessageBox.Show("左缩进值对子面板无意义，请选择一个普通公式！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }

            if (Globals.selectedEditor.SelectedExpression.ParentPanel == null)
            {
                MessageBox.Show("意外情况，未能取得公式的父级子面板！操作未执行。", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }

            if (Globals.selectedEditor.SelectedExpression.ParentPanel.Direction == Orientation.Horizontal)
            {
                MessageBox.Show("此公式的父面板是指向水平方向的，左缩进值无意义！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }

            return true;
        }

        public static void SetIndentLevel()
        {
            if (CheckForGetAndSetIndent() == false) return;

            Globals.selectedEditor.SelectedExpression.Indent = Globals.formatIndentLevel;
        }

        public static void GetIndentLevel()
        {
            if (CheckForGetAndSetIndent() == false) return;

            Globals.formatIndentLevel = Globals.selectedEditor.SelectedExpression.Indent;
        }

        public static bool CanIndent(Expressions.Expression ex)
        {
            if (ex == null) return false;
            if (ex is Expressions.SubPanel) return false;

            if (ex.ParentPanel == null) return false;

            if (ex.ParentPanel.Direction == Orientation.Horizontal) return false;

            if (ex.Indent >= 6) return false;

            return true;
        }

        public static bool CanOutdent(Expressions.Expression ex)
        {
            if (ex == null) return false;
            if (ex is Expressions.SubPanel) return false;

            if (ex.ParentPanel == null) return false;

            if (ex.ParentPanel.Direction == Orientation.Horizontal) return false;

            if (ex.Indent <= 0) return false;

            return true;
        }

        public static void Outdent()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression.Indent <= 0)
            {
                //这个消息框很烦人！！
                //MessageBox.Show("已到最小缩进级别！", Globals.appName,
                //MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                return;//子面板不支持缩进。
            }

            if (curEx is Expressions.BaseText && curEx.Indent > 0)
            {
                Expressions.BaseText curBt = curEx as Expressions.BaseText;
                if (curBt.IsCollapsed)
                {
                    MessageBox.Show("折叠的标题文本块不能更改缩进级别。此外“剪切、粘贴、删除、平移、拖动”等操作亦被禁止。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            if (curEx.Parent is StackPanel)
            {
                StackPanel spanel = curEx.Parent as StackPanel;

                if (spanel.Orientation == Orientation.Vertical)
                {
                    if (curEx.Align == Expressions.Expression.Alignment.RightOrBottom)
                    {
                        return;//右对齐时，Indent或Outdent均无效。
                    }

                    ModifingItem mi = new ModifingItem();
                    Action action = new Action(curEx.ExID, -1, curEx.GetType().Name, "Indent",
                        curEx.Indent.ToString(), (curEx.Indent - 1).ToString());
                    curEx.Indent -= 1;
                    mi.Add(action);

                    if (curEx.Indent <= 0)
                    {
                        Globals.MainWindow.btnOutdent.IsEnabled = false;
                    }
                    else
                    {
                        Globals.MainWindow.btnOutdent.IsEnabled = true;
                    }

                    Globals.MainWindow.btnIndent.IsEnabled = true;

                    //如果是文本块，要看看是否从４级缩进返回３级缩进。
                    //因为３级缩进正好是４级标题的位置。
                    if (curEx.Indent <= 3)
                    {
                        if (curEx is Expressions.BaseText)
                        {
                            Expressions.BaseText bt = curEx as Expressions.BaseText;
                            if (bt.IsHeaderText)
                            {
                                Action actFontSizeLevel = new Action(bt.ExID, -1,
                                    bt.GetType().Name,
                                    "FontSizeLevel", bt.FontSizeLevel.ToString(), "1");
                                //正文的“FontSizeLevel”是０级，而４级标题则是１级。
                                bt.FontSizeLevel = 4 - bt.Indent;

                                mi.Add(actFontSizeLevel);

                                //只有isHesaText为真时才有必要控制加粗。
                                //if (bt.FontSizeLevel % 2 == 0)
                                if (bt.FontSizeLevel == 4)
                                {
                                    if (bt.FontWeight != FontWeights.Bold)
                                    {
                                        Action actFontWeight = new Action(bt.ExID, -1,
                                            bt.GetType().Name, "FontWeight",
                                            bt.FontWeight.ToString(),
                                            FontWeights.Bold.ToString());
                                        bt.FontWeight = FontWeights.Bold;
                                        mi.Add(actFontWeight);
                                    }
                                }
                                else
                                {
                                    if (bt.FontWeight != FontWeights.Normal)
                                    {
                                        Action actFontWeight = new Action(bt.ExID, -1,
                                            bt.GetType().Name, "FontWeight",
                                            bt.FontWeight.ToString(),
                                            FontWeights.Normal.ToString());
                                        bt.FontWeight = FontWeights.Normal;
                                        mi.Add(actFontWeight);
                                    }
                                }

                                //重复。BaseText本身就会调用这个方法。
                                //bt.ParentPanel.RefreshAutoNumbersOfBaseText();
                                bt.RefreshText();
                            }

                            Globals.MainWindow.tbtnBold.IsChecked =
                                (bt.FontWeight == FontWeights.Bold ? true : false);
                            Globals.MainWindow.tbtnItalic.IsChecked =
                                (bt.FontStyle == FontStyles.Italic ? true : false);

                            Globals.MainWindow.RefreshFontSizeLevel(bt.FontSizeLevel);
                        }
                    }

                    // 刷新父面板中所有可能存在的树型线。
                    if (curEx.ParentPanel != null)
                    {
                        curEx.ParentPanel.RefreshTreeLinesOfHeaderText();
                    }

                    curEx.Editor.Do(mi);
                }
                else
                {
                    //水平子面板中，不支持左缩进。
                    MessageBox.Show("在垂直子面板中的公式才支持“减少左缩进”操作！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
    }
}
